package com.daxidi.cn.controller;

import com.daxidi.cn.dto.LoginDTO;
import com.daxidi.cn.service.UserService;
import com.daxidi.cn.vo.UserVO;


import com.daxidi.cn.entity.User;
import com.daxidi.cn.service.IUserService;
import com.daxidi.cn.utils.ReturnResult;
import com.daxidi.cn.utils.ReturnResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 通过微信小程序登录时，记录微信小程序的信息，在表中增加用户注册类型
 * 前端控制器
 * </p>
 *
 * @author wsh
 * @since 2020-11-09
 */
@Log4j
@Api(tags = "用户模块接口")
@RestController
@RequestMapping("/api/v1/union/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "用户模块  添加接口")
    @ApiResponses({
            @ApiResponse(code = 100, message = "添加成功"),
            @ApiResponse(code = 101, message = "添加失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @PostMapping("/")
    public ReturnResult addUser(@RequestBody User user) {
        try {
            userService.addUser(user);
            return ReturnResultUtils.returnSuccess();
        } catch (Exception e) {
            log.error("添加失败" + e);
            return ReturnResultUtils.returnFailMsg(101, "添加失败");
        }
    }


    @ApiOperation(value = "用户模块  删除接口")
    @ApiResponses({
            @ApiResponse(code = 102, message = "删除成功"),
            @ApiResponse(code = 103, message = "删除失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @DeleteMapping("/{id}")
    public ReturnResult deleteUserById(@PathVariable("id") Integer id) {
        try {
            userService.deleteUserById(id);
            return ReturnResultUtils.returnSuccess();
        } catch (Exception e) {
            log.error("删除失败" + e);
            return ReturnResultUtils.returnFailMsg(103, "删除失败");
        }
    }


    @ApiOperation(value = "用户模块  修改接口")
    @ApiResponses({
            @ApiResponse(code = 104, message = "修改成功"),
            @ApiResponse(code = 105, message = "修改失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @PutMapping("/{id}")
    public ReturnResult updateUserById(@RequestBody User user) {
        try {
            user.setUpdatedAt(new Date());
            userService.updateUserById(user);
            return ReturnResultUtils.returnSuccess();
        } catch (Exception e) {
            log.error("修改失败" + e);
            return ReturnResultUtils.returnFailMsg(105, "修改失败");
        }
    }


    @ApiOperation(value = "用户模块  分页查询接口")
    @ApiResponses({
            @ApiResponse(code = 106, message = "查询成功"),
            @ApiResponse(code = 107, message = "查询失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @GetMapping("/")
    public ReturnResult selectUsers(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit) {
        try {
            Map map = userService.selectUsersPage(page, limit);
            return ReturnResultUtils.returnSuccess(map);
        } catch (Exception e) {
            log.error("查询失败" + e);
            return ReturnResultUtils.returnFailMsg(107, "查询失败");
        }
    }
}
